#include "imageencode.h"
#include "QObject"
#include <QDebug>
//负责图片编码
imageEncode::imageEncode(QObject *parent) : QObject(parent)
{

}
QByteArray imageEncode::imageTobase64(QString filename)
{
    //把图片传进来并生成图片对象
    QImage img(filename);
    //用字节数组保存QString，然后用QByteArray的方法转成base64
    QByteArray bianma;
    QBuffer buf(&bianma);
    buf.open(QIODevice::WriteOnly);
    //把img写入到QBuffer
    img.save(&buf,"png");
    QByteArray base64=bianma.toBase64();
    //官方文档说还需要把图片Urlencode（这里看不懂）其实不用！
    QTextCodec* code=QTextCodec::codecForName("UTF-8");
    QByteArray imgData=code->fromUnicode(base64).toPercentEncoding();
    return imgData;
    //return base64;
}
